package punch.client;

import cc.chenwenxi.api.commons.entity.timesheet.PunchEvent;
import cc.chenwenxi.api.commons.entity.timesheet.PunchType;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import okhttp3.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

@RequiredArgsConstructor
public class Cli {

    public void in() throws IOException {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("text/plain");
        RequestBody body = RequestBody.create(mediaType, "");
        Request request = new Request.Builder()
                .url("http://om.weoa.com/Attendance/Save?type=begin&city=54&building=206&loginCode={换成自己的}&remark=&method=click")
                .method("POST", body)
                .addHeader("Connection", "keep-alive")
                .addHeader("Content-Length", "0")
                .addHeader("Pragma", "no-cache")
                .addHeader("Cache-Control", "no-cache")
                .addHeader("Accept", "application/json, text/plain, */*")
                .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36")
                .addHeader("Origin", "http://om.weoa.com")
                .addHeader("Referer", "http://om.weoa.com/Home")
                .addHeader("Accept-Language", "zh-CN,zh;q=0.9")
                .addHeader("Cookie", "WB_LOGIN_TICKET=hm7yvBdFTbtBeg6J9wPKbc1EX1639535805vVpkIToOWK9zqNOAsy3AEtZ7E; WB_LOGIN_TYPE=um; WB_OM_LOGINTICKET=ixeZC%2fL3qblAVy7Q5wT1%2fi34myOe%2fIeZWc4rV3zfEXXy4HfsNIuEZUyK9PeicU1YaS23%2b6yKe7nfyNjm%2bXIyOHL8DCNq%2bB4rizTJtm%2bs0d3p9vDQipSw7wik0eYT8CiE0t2kjwigxrC7OYc%2b3tEj3DdumEDQmZdcOn00v37sbmkEhaK1RBDWDKc6UXp%2b64gRv1%2fY%2fZMsDgx%2bK6oZcPreKsd8TuhnV5pQeyjE6eMfqfFG8wr0NWSGCiGmseHXuhFulwSbGgxI9bDLatddnzAhIukyrSEe%2bwo9oJBSW7GUeCSM0P5Eq8G%2fqNcFdrBRjALtqTD6G%2fTK6x3bCun3izWfvM3FB7fH67nPcLJcEEiNaSHwfSQrC7P3Lao6lDPnkau6cSUFBf4ztToXvtG5RS0NOIwrpAqTDOqv")
                .build();
        Response response = client.newCall(request).execute();
        String json = response.body().string();
        String format = StrUtil.format("签入:{}", json);
        PunchEvent punchEvent = new PunchEvent();
        punchEvent.setPunchType(PunchType.in);
        punchEvent.setPunchJson(json);
        ThreadUtil.sleep(2, TimeUnit.SECONDS);
    }

    public void out() throws IOException {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        MediaType mediaType = MediaType.parse("text/plain");
        RequestBody body = RequestBody.create(mediaType, "");
        Request request = new Request.Builder()
                .url("http://om.weoa.com/Attendance/Save?type=end&city=54&building=206&loginCode={换成自己的}&remark=&method=click")
                .method("POST", body)
                .addHeader("Connection", "keep-alive")
                .addHeader("Content-Length", "0")
                .addHeader("Pragma", "no-cache")
                .addHeader("Cache-Control", "no-cache")
                .addHeader("Accept", "application/json, text/plain, */*")
                .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36")
                .addHeader("Origin", "http://om.weoa.com")
                .addHeader("Referer", "http://om.weoa.com/Home")
                .addHeader("Accept-Language", "zh-CN,zh;q=0.9")
                .addHeader("Cookie", "WB_LOGIN_TICKET=hm7yvBdFTbtBeg6J9wPKbc1EX1639535805vVpkIToOWK9zqNOAsy3AEtZ7E; WB_LOGIN_TYPE=um; WB_OM_LOGINTICKET=ixeZC%2fL3qblAVy7Q5wT1%2fi34myOe%2fIeZWc4rV3zfEXXy4HfsNIuEZUyK9PeicU1YaS23%2b6yKe7nfyNjm%2bXIyOHL8DCNq%2bB4rizTJtm%2bs0d3p9vDQipSw7wik0eYT8CiE0t2kjwigxrC7OYc%2b3tEj3DdumEDQmZdcOn00v37sbmmKlkOkVFy6sVIaUKZfUOm46NDzd4VCZDBk7LFIaWHqIkrYcXKIcE8ZfsuuoornfAJKsqmG7XzZSlqnoX9BFVHaENsGg1gpleGUouWbXEQ12yp%2fbGPVdPfSmhQqt6XQO7U%2braX%2fegxnZSAYyu2oZ3IzVPP4Z%2bjwl2k1n9rpccHxyDRvLjo7ZF1BAcSxNA38KabKxYrI1yymvHemURwYfDlKsbtEu%2bwnfV%2bpxWSdhxVgeXgMZKQMEIcl; WB_OM_LOGINTICKET=ixeZC%2fL3qblAVy7Q5wT1%2fi34myOe%2fIeZWc4rV3zfEXXy4HfsNIuEZUyK9PeicU1YaS23%2b6yKe7nfyNjm%2bXIyOHL8DCNq%2bB4rizTJtm%2bs0d3p9vDQipSw7wik0eYT8CiE0t2kjwigxrC7OYc%2b3tEj3DdumEDQmZdcOn00v37sbmmKlkOkVFy6sZbFrVsVr76EMbiGn4TvG60vlA5CXSCHbXCs0rkCqjPPY7Eexwrlt0MjetSGYz59oQwK%2frd27ZdNok8Pbcbv3eIaGWuHdG%2f6BqSeiSKsa%2bdKmBr4DiOAStzgFW3oi9zq%2bkvMsQxIC8jxv8ImJOS1erQqnFgQLZLJ%2bj9Ld%2f7WZ93idKztHedZ9zAxg%2fvRnj%2b9TeLj7E2byLzOdznIAoO3opNLN%2fCdBC478%2bMJv3878%2blD")
                .build();
        Response response = client.newCall(request).execute();
        String json = response.body().string();
        String format = StrUtil.format("签出:{}", json);
        PunchEvent punchEvent = new PunchEvent();
        punchEvent.setPunchType(PunchType.out);
        punchEvent.setPunchJson(json);
        ThreadUtil.sleep(2, TimeUnit.SECONDS);
    }

}
